cd "C:\Users\pgi1\Dropbox\Coal 2021 _ Joe Peter\Bank Regressions\data"

use  matched_dataset_deal,clear
bysort packageid ultimateparentid : keep if _n==1

destring minpolicyyear projectban companyban enhancedreview, force replace

gen treated=(year>minpolicyyear)
gen treated_project=(year>projectban)
gen treated_company=(year>companyban)
gen treated_enhanced=(year>enhancedreview)
sum

bysort ultimateparentid: egen treated_ever=max(treated)
bysort ultimateparentid: egen treated_project_ever=max(treated_project)
bysort ultimateparentid: egen treated_company_ever=max(treated_company)
bysort ultimateparentid: egen treated_enhanced_ever=max(treated_enhanced)

gen trend=year-2000
gen diff_trend_treated=trend*treated_ever
gen diff_trend_treated_project=trend*treated_project_ever
gen diff_trend_treated_company=trend*treated_ever*treated_company_ever
gen diff_trend_treated_enhanced=trend*treated_enhanced_ever

gen log_assets=log(total_assets)


label var log_assets "Log Assets"

tab year, gen (y_)

label var treated "Any Policy"
label var treated_company "Company Policy"
label var treated_project "Project Policy"
label var treated_enhanced "Enhanced Policy"


label var diff_trend_treated "Differential Trend for Treated"
label var diff_trend_treated_company "Differential Trend for Treated"
label var diff_trend_treated_project "Differential Trend for Treated"
label var diff_trend_treated_enhanced "Differential Trend for Treated"

gen deal_amount_mtr_ln=ln(deal_amount_converted+1)
gen log_allindrawn_max=log(allindrawn_max)
gen log_allindrawn_imp_max=log(allindrawn_imp_max)
gen log_allinundrawn_imp_max=log(allinundrawn_imp_max)
gen log_fees_max=log(fees_max)
gen log_maturity=log(maturity)


gen log_deals5=log(1+deals5)
gen log_deals10=log(1+deals10)

gen treated_log_deals5=treated*log_deals5
gen treated_log_deals10=treated*log_deals10

label var log_deals5 "Relationship"
label var log_deals10 "Relationship"
label var treated_log_deals10 "Relationship * Any Policy"
label var treated_log_deals5 "Relationship * Any Policy"


label var log_assets "Log Assets"
label var asset_growth_3 "Asset Growth"
label var deal_amount_converted "Deal Amount (\$ millions)"
label var maturity "Maturity"
label var revolver "Has Revolver"
label var secured_loan "Secured Loan"
label var allindrawn_max "Spread over Libor"
label var allindrawn_imp_max "Spread over Libor"

label var loans_related_lag3_ln  "Ln Coal Loans"
label var loans_total_lag3_ln "Ln Total Loans"


gen treated_deposit=mtrdepositshare*treated
gen treated_articles=relevant_article*treated

label var treated_deposit "Treated x Share of Deposits"
label var treated_articles "Treated x Articles"



gen has_article=(relevant_article>0 )
replace has_article=. if mi(relevant_article)
label var has_article "Media Attention"
gen treated_has_article=has_article*treated
label var treated_has_article "Media Attention x Any Policy"


drop has_deposits
gen has_deposits=mtrdepositshare>0.01
replace has_deposits=. if mi(mtrdepositshare)
label var has_deposits "Local Deposits"
gen treated_has_deposits=has_deposits*treated
label var treated_has_deposits "Local Deposits x Any Policy"

gen large_bank=0
replace large_bank= 1 if (total_assets>20314.98 )
replace large_bank=. if mi(total_assets)
label var large_bank "Large Lender"
gen treated_large=large_bank*treated
label var treated_large "Large Lender x Any Policy"

gen has_relationship=deals5>0
replace has_relationship=. if mi(deals5)
label var has_relationship "Relationship Lender"
gen treated_has_relationship=has_relationship*treated
label var treated_has_relationship "Relationship Lender x Any Policy"

compress
bysort packageid ultimateparentid : keep if _n==1

save deals_vars_dataset, replace

exit
